package cordova.plugin.dascomdemo;

import com.dascom.print.PrintCommands.CPCL;
import com.dascom.print.PrintCommands.ZPL;
import com.dascom.print.Transmission.BluetoothPipe;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;

import static com.dascom.print.Utils.Unit.DPI_203.CM;
import static com.dascom.print.Utils.Unit.DPI_203.MM;

/**
 * This class echoes a string called from JavaScript.
 */
public class dascomdemo extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("coolMethod")) {
            String message = args.getString(0);
            String message2 = args.getString(1);
            this.coolMethod(message, message2 ,callbackContext);
            return true;
        }
        if (action.equals("printCPCLQRCode")) {
            String message = args.getString(0);
            String message2 = args.getString(1);
            this.coolMethod(message, message2 ,callbackContext);
            return true;
        }
        if (action.equals("printZPLQRCode")) {
            String message = args.getString(0);
            String message2 = args.getString(1);
            this.coolMethod(message, message2 ,callbackContext);
            return true;
        }
        if (action.equals("CPCL_printQRCode")) {
            printQRCode(args.getString(0),args.getInt(1), args.getInt(2),args.getInt(3)
                    ,args.getInt(4), args.getInt(5), args.getString(6), args.getString(7),callbackContext);
                return true;
        }


        return false;
    }

    private void coolMethod(String message,String message2, CallbackContext callbackContext) {
        if (message != null && message2!=null && message.length() > 0) {
            String rmsg=printCPCLQRCode(message,message2);
            callbackContext.success(rmsg);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void printZPLQRCode(String mac_adress,String qrcode, CallbackContext callbackContext) {
        if (mac_adress != null && qrcode!=null && mac_adress.length() > 0) {
            callbackContext.success(mac_adress);
            printZPLQRCode(mac_adress,qrcode);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void printCPCLQRCode(String mac_adress,String qrcode, CallbackContext callbackContext) {
        if (mac_adress != null && qrcode!=null && mac_adress.length() > 0) {
            callbackContext.success(mac_adress);
            printCPCLQRCode(mac_adress,qrcode);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    /**
     * 打印二维码ZPL
     * */
    private void printZPLQRCode(String mac_adress,String qrcode){

        try {
            String item=qrcode.split("/")[0];
            BluetoothPipe pipe= new BluetoothPipe(mac_adress);
            ZPL zpl=new ZPL(pipe);
            zpl.setLabelStart();
            zpl.setLabelWidth(75 * MM);
            zpl.setLabelLength(CM);
            zpl.printText(0, 0, 2, 2, item);
            zpl.setLabelEnd();

            zpl.setLabelStart();
            zpl.setLabelWidth(75 * MM);
            zpl.setLabelLength(3 * CM);
            //打印二维码,大小3，纠错级别 L
            zpl.printText(0, 0, 1, 1, "大小：5，纠错级别:Q");
            zpl.printQRCode(0, 5 * MM, 5, 'Q', qrcode);

            zpl.setLabelEnd();


        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private void  printQRCode(String mac_adress,
                              int len, int quantity,int x, int y, int size, java.lang.String error, java.lang.String qrcode, CallbackContext callbackContext){

        try {

            BluetoothPipe pipe= new BluetoothPipe(mac_adress);

            CPCL cpcl=new CPCL(pipe);

            cpcl.setLabelStart(5 * CM, 1);
            cpcl.printQRCode(5 * MM, 1, 6, "Q", qrcode);
            cpcl.formFeed();
            cpcl.setLabelEnd();

        } catch (IOException e) {
            e.printStackTrace();
            callbackContext.error(e.getMessage());
        }
        callbackContext.success("print ok");

    }

    /**
     * 打印二维码CPCL
     * */
    private String printCPCLQRCode(String mac_adress,String qrcode){

        try {
            BluetoothPipe pipe= new BluetoothPipe(mac_adress);
            String item=qrcode.split("/")[0];
            String itemrq=qrcode.split("/")[3];
            itemrq=itemrq.replaceAll("Y","0");
            itemrq=itemrq.replaceAll("M","1");
            itemrq=itemrq.replaceAll("X","2");
            itemrq=itemrq.replaceAll("E","3");
            itemrq=itemrq.replaceAll("C","4");
            itemrq=itemrq.replaceAll("N","5");
            itemrq=itemrq.replaceAll("F","6");
            itemrq=itemrq.replaceAll("L","7");
            itemrq=itemrq.replaceAll("R","8");
            itemrq=itemrq.replaceAll("K","9");

            CPCL cpcl=new CPCL(pipe);
            cpcl.setLabelStart(5 * CM,1);
            cpcl.printQRCode(15 * MM, 3 * MM, 6, "M", qrcode);
            cpcl.setLabelStart(5 * CM,1);
            cpcl.printText(20 * MM, 30 * MM, 0, 5, 0, item+"-"+itemrq);
            cpcl.formFeed();
            cpcl.setLabelEnd();
            return "print ok";
        } catch (IOException e) {

            return e.getMessage();

        }


    }
}
